using System;
using ProtoScratch.Common;

namespace TestSpace
{
	public class ClientSample : ScratchEvents
	{
		public override void NewConnection()
		{
			Console.WriteLine("Client connected.");
			Send(new ScratchMessage(0, 1000, 0));
		}
		
		public override void ConnectionLost()
		{
			Console.WriteLine("Client disconnected.");
		}
		
		public override void MessageReceived(ScratchMessage Message)
		{
			Console.WriteLine("Client received on Channel " + Message.Channel + ": Primary " + Message.PrimaryCommand + " Secondary " + Message.SecondaryCommand);
			
			switch (Message.PrimaryCommand)
			{
			case 2000:
				Console.WriteLine("received 2000 sending 3000");
				var reply = new ScratchMessage(0, 3000, 0);
				reply.pack("This string has been packed in the message");
				reply.pack((Int16)10);
				reply.pack((Int32)42);
				reply.Compressed = true;
				reply.Encrypted = true;
				Send(reply);
				break;
				
			case 3000:
				Console.WriteLine("received 3000 sending 4000");
				string s = "";
				Int16 i16 = 0;
				Int32 i32 = 0;
				
				if (Message.get(ref s))
					Console.WriteLine(s);
				if (Message.get(ref i16))
					Console.WriteLine(i16);
				if (Message.get(ref i32))
					Console.WriteLine(i32);
				
				Send(new ScratchMessage(0, 4000, 0));
				break;
				
			case 4000:
				Console.WriteLine("End of sequence.");
				System.Threading.Thread.Sleep(5000);
				Close();
				break;
				
			default:
				Console.WriteLine("Starting sequence.");
				Send(new ScratchMessage(0,2000,3000));
				break;
			}
		}
	}
}

